#include "ItemComponent.h"
#include "InventoryComponent.h"
#include "Level/GameObject.h"

mtBEGIN(ItemComponentDef)
	mtBASE(ActivatorDef);
	mtCONSTRUCTOR();
	mtVARIABLE(itemId);
mtEND

mtBEGIN(ItemComponent)
	mtBASE(Activator);
	mtCONSTRUCTOR();
	mtMETHOD(initWith);
mtEND

bool ItemComponent::initWith(ItemComponentDef* def)
{
	Item* item = Item::getItem(def->itemId);
	_item.item = item;
	_item.count = 1;
	return true;
}

void ItemComponent::onActivate( GameObject* actor )
{
	auto inventory = actor->getComponent<InventoryComponent>();
	if (inventory)
	{
		inventory->getInventoryPtr()->addItem(_item.item, _item.count);
		getAssembly()->destroy();
	}
}
